'use strict';

const Controller = require('../../../core/common');

class QuestionsController extends Controller {

  async index() {
    const { ctx } = this;

    const attributes = ['id', 'title', 'brief', 'createdAt', 'updatedAt'];

    const data = await ctx.service.v1.questions.findAll({ attributes });

    this.success(data);
  }

  async create() {
    const { ctx } = this;

    const data = await ctx.service.v1.questions.create(ctx.request.body);

    this.success(data);
  }

  async update() {
    const { ctx } = this;

    const { id = '' } = ctx.params;
    const { title, brief, order } = ctx.request.body;

    const payload = {}
    title && Object.assign(payload, { title });
    brief && Object.assign(payload, { brief });
    order && Object.assign(payload, { order });

    const data = await ctx.service.v1.questions.update(payload, { where: { id } });

    this.success(data);
  }

  async destroy() {
    const { ctx } = this;

    const { id = '' } = ctx.params;

    const data = await ctx.service.v1.questions.destroy({
      where: { id: id.indexOf(',') === -1 ? id : { '$in': id.split(',') } }
    });

    this.success(data);
  }

}

module.exports = QuestionsController;